var ajaxUrl="";
var live={
    imgInit:function () {
        $.get(
            application.contextPath+"/org/getLiveDet/"+liveId,
            function (res) {
                if(res.isSuccess){
                    $("#liveName").val(res.data.liveName);
                    $("#liveUrl").val(res.data.liveUrl);
                    $("#ObjId").val(orgId);
                    $("#liveId").val(liveId);
                    var temp = [];
                    //循环读取图片
                    if (res.data.attachUrl != null && res.data.attachUrl != "undefined") {
                        temp.push(res.data.attachUrl);
                        //初始化图片展示
                        $("#case2").attr("data-value", temp);
                    }
                }else {
                    layer.open({
                        title:"加载失败!",
                        content:'请求加载失败!'
                    });
                }
                $("#case2").upload(
                    function(_this,data){
                        layer.msg("图片链接："+data,{time: 5000},function(){});
                    }
                );
                // $("#case2").attr("action","/org/addLiveAtt/"+liveId);
            }
        );
    },
    commit:function (form) {
        var data = {};
        var array = $(form).serializeArray();
        for(var index in array){
            data[array[index]['name']] = array[index]['value']
        }
        data.parentId=orgId;
        if(type =='upd'){
            data.liveId=liveId;
        }else if(type == 'show'){
            return false;
        }
        if(!jsEcc.isNull(data.liveName)){
            layer.msg('直播名称不能为空',{icon:5});
            return false;
        }
        if(!jsEcc.isNull(data.liveUrl)){
            layer.msg('直播地址不能为空',{icon:5});
            return false;
        }
        $.ajax({
            url : application.contextPath+ajaxUrl,
            type:"post",
            contentType:"application/json; charset=utf-8",
            dataType: "json",
            data:JSON.stringify({orgLive:data}),
            success:function (res) {
                 if(res.isSuccess){
                     if(type=='add'){
                         $("#liveId").val(res.data);
                         liveId=res.data;
                         $("#topImgBut").show();
                         $("#case2").attr("action","/org/addLiveAtt/"+res.data);
                         ajaxUrl="/org/updateLive";
                         type='upd';
                         layer.msg("新增成功",{icon:1})
                     }else {
                         layer.msg("修改成功",{icon:1})
                     }
                     var opt={
                         url:application.contextPath+"/org/getLiveList/"+$("#ObjId").val(),
                         query:{size:5,page:$('#liveList').bootstrapTable('getOptions').pageNumber},
                         silent:true
                     };
                     $('#liveList').bootstrapTable('refresh',opt);
                 }else {
                     layer.open({
                         title:"加载失败!",
                         content:res.message
                     });
                 }
            }
        });
        return false;
    },

};

$(document).ready(function(){
   switch (type) {
       case 'add' :
           ajaxUrl="/org/addLive";
           $("#case2").upload(
               function(_this,data){
                   layer.msg("图片链接："+data,{time: 5000},function(){});
               }
           );
           $("#topImgBut").hide();
           break;
       case 'show':
           ajaxUrl="";
           $("#topBut").hide();
           live.imgInit();
           break;
       case 'upd':
           ajaxUrl="/org/updateLive";
           live.imgInit();
           break;
   }
});